# -*- coding: utf-8 -*-

import logging
import traceback

from GKModel import Index
from GKModel import OTSHelper
from utils import build_json
from utils import parse_event


def handler(event, context):
    logger = logging.getLogger()
    try:
        http_method, query_parameters, path_parameters, body, client_token, deploy_config = parse_event(event)
        ots_client = OTSHelper.create_client()
        index = Index.new(
            ots_client=ots_client,
            deploy_config=deploy_config,
            logger=logger,
            client_token=client_token
        )
        if http_method == 'GET':
            data, client_token = index.get_index()
        else:
            data = {}
            client_token = ''
        return build_json(0, 'Success', data, client_token)

    except ValueError as e:
        logger.error(e)
        traceback.print_exc()
        return build_json(e.args[0], e.args[1], None)
    except Exception as e:
        logger.error(e)
        traceback.print_exc()
        return build_json(-1, str(e), None)

